using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Web;
using iPhOwa.Exchange;
using iPhOwa.Exchange.ContentClasses;
using iPhOwa.Exchange.Managers;
using iPhOwa.Web.Properties;

namespace iPhOwa.Web
{
	/// <summary>
	/// View a particular message.
	/// </summary>
	public partial class ViewMessage : iPhOwaPage
	{
		/// <summary>
		/// Page_Load Event
		/// </summary>
		[SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")]
		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				Message message = MessageManager.Get(Path, OwaSession);

				if(message == null)
				{
					throw new ApplicationException("Expected message, but got something else.");
				}

				if(message.ParentName != null)
				{
					Folder parentFolder = FolderManager.Get(message.ParentName, OwaSession);

					if(parentFolder != null)
					{
						parentNameLabel.Text = parentFolder.DisplayName;
						backPanel.Visible = true;

						// TODO: This should to a basic folder page that determines the folder type...
						backPanel.Href = string.Format(CultureInfo.InvariantCulture, "ViewFolder.aspx?path={0}", parentFolder.Href);
					}
				}

				if(!message.Read)
				{
					MessageManager.MarkAsRead(Path, OwaSession);
				}

				Title = string.Format(CultureInfo.CurrentUICulture, "{0} - {1}", message.Subject, Settings.Default.Title);

				fromLabel.Text = message.FromName;
				toLabel.Text = message.DisplayTo;
				receivedLabel.Text = message.DateReceived.ToFriendlyShortDateTime();
				subjectLabel.Text = message.Subject;
				highPriorityImage.Visible = message.Priority == MessagePriority.High;
				lowPriorityImage.Visible = message.Priority == MessagePriority.Low;
				flaggedImage.Visible = message.IsFlagged && !message.FlagCompleted;
				completedImage.Visible = message.IsFlagged && message.FlagCompleted;
				bodyLabel.Text = BuildHtmlDescription(message.TextDescription);

				replyLink.NavigateUrl = string.Format(CultureInfo.InvariantCulture, "~/ComposeMessage.aspx?action=reply&path={0}", HttpUtility.UrlEncode(message.Href.ToString()));
				replyAllLink.NavigateUrl = string.Format(CultureInfo.InvariantCulture, "~/ComposeMessage.aspx?action=replyall&path={0}", HttpUtility.UrlEncode(message.Href.ToString()));
				forwardLink.NavigateUrl = string.Format(CultureInfo.InvariantCulture, "~/ComposeMessage.aspx?action=forward&path={0}", HttpUtility.UrlEncode(message.Href.ToString()));

				if(message.HasAttachment)
				{
					attachmentsRow.Visible = true;

					attachmentsList.DataSource = MessageManager.GetAttachments(message.Href, OwaSession);
					attachmentsList.DataBind();
				}

				if(!string.IsNullOrEmpty(message.DisplayCC))
				{
					ccRow.Visible = true;
					ccLabel.Text = message.DisplayCC;
				}
			}
		}

		/// <summary>
		/// Delete link event.
		/// </summary>
		[SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")]
		protected void DeleteLink_Click(object sender, EventArgs e)
		{
			Message message = MessageManager.Get(Path, OwaSession);

			if(message == null)
			{
				throw new ApplicationException("Expected message, but got something else.");
			}

			Folder parentFolder = FolderManager.Get(message.ParentName, OwaSession);

			MessageManager.Move(Path, DeletedItemsUrl, OwaSession);

			Response.Redirect(string.Format(CultureInfo.InvariantCulture, "ViewFolder.aspx?path={0}", parentFolder.Href));
		}
	}
}